﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using TERP.WaterClient.Common;

namespace TERP.WaterClient.Entities.Auth
{
	public class UserCollection : List<User>
	{
		private DataTable _dtbData;

		public UserCollection()
			: base()
		{
		}

		/// <summary>
		/// 
		/// </summary>
		public DataTable DataTableUser
		{
			get
			{
				return _dtbData;
			}
			set
			{
				_dtbData = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="soCode"></param>
		/// <returns></returns>
		public User FindUser(string userCode)
		{
			foreach (User user in this)
			{
				if (user.UserCode.Equals(userCode))
					return user;
			}
			return null;
		}

		/// <summary>
		/// 
		/// </summary>
		public void FillToDataTable()
		{
			_dtbData = new DataTable(DbTable.Tbl_User);
			_dtbData.Columns.AddRange(new DataColumn[] { 
		        new DataColumn(TblUser.User_Code,typeof(string)),
				new DataColumn(TblUser.User_Name,typeof(string)),
                //new DataColumn(TblUser.Dealer_Code,typeof(string)),
				new DataColumn(TblUser.User_Password,typeof(string)),
				new DataColumn(TblUser.Flag_Active,typeof(string)),
                //new DataColumn(TblUser.Flag_Sys_Admin,typeof(string))
			});
			DataRow dtRow;
			foreach (User grp in this)
			{
				dtRow = _dtbData.NewRow();
				dtRow[TblUser.User_Code] = grp.UserCode;
				dtRow[TblUser.User_Name] = grp.UserName;
                //dtRow[TblUser.Dealer_Code] = grp.DealerCode;
				dtRow[TblUser.User_Password] = grp.UserPassword;
                //dtRow[TblUser.Flag_Sys_Admin] = grp.FlagSysAdmin;
				dtRow[TblUser.Flag_Active] = grp.FlagActive;
				//
				_dtbData.Rows.Add(dtRow);
			}
			_dtbData.AcceptChanges();
		}
	}
}
